package com.ht.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ht.domain.bo.UserCollectBO;
import com.ht.domain.po.TbKnowledge;
import com.ht.domain.po.TbUserCollect;
import com.ht.mapper.KnowledgeMapper;
import com.ht.mapper.UserCollectMapper;
import com.ht.service.UserCollectService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户收藏表service
 */
@Service
public class UserCollectServiceImpl implements UserCollectService {

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @Autowired
    private UserCollectMapper userCollectMapper;

    @Autowired
    private KnowledgeMapper knowledgeMapper;

    /**
     * 收藏
     * @param knowledgeId
     * @param userId
     * @return
     */
    @Override
    public Integer addUserCollect(Long knowledgeId, Long userId) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("knowledgeId",knowledgeId);
        jsonObject.put("userId",userId);
        //通过发送rocketMQ消息进行用户所选知识的收藏
        rocketMQTemplate.convertAndSend("user-collect-topic",jsonObject.toJSONString());
        return 1;
    }

    /**
     * 根据用户id查询用户的前二十条收藏
     * @param userId
     * @return
     */
    @Override
    public List<UserCollectBO> findCollectListTwentyByUserId(Long userId) {
        Page<TbUserCollect> userCollectPage = userCollectMapper.selectPage(new Page<>(0, 20), new QueryWrapper<TbUserCollect>().eq("user_id",userId));
        List<TbUserCollect> userCollectList = userCollectPage.getRecords();
        List<UserCollectBO> userCollectBOList = new ArrayList<>();
        for (TbUserCollect userCollect:userCollectList) {
            UserCollectBO userCollectBO = new UserCollectBO();
            TbKnowledge knowledge = knowledgeMapper.selectById(userCollect.getKnowledgeId());
            BeanUtils.copyProperties(userCollect,userCollectBO);
            userCollectBO.setKnowledgeName(knowledge.getKnowledgeName());
            userCollectBOList.add(userCollectBO);
        }
        return userCollectBOList;
    }

    /**
     * 根据用户id查询用户的全部收藏
     * @param userId
     * @return
     */
    @Override
    public List<UserCollectBO> findCollectListByUserId(Long userId) {
        List<TbUserCollect> userCollectList = userCollectMapper.selectList(new QueryWrapper<TbUserCollect>().eq("user_id",userId));
        List<UserCollectBO> userCollectBOList = new ArrayList<>();
        for (TbUserCollect userCollect:userCollectList) {
            UserCollectBO userCollectBO = new UserCollectBO();
            TbKnowledge knowledge = knowledgeMapper.selectById(userCollect.getKnowledgeId());
            BeanUtils.copyProperties(userCollect,userCollectBO);
            userCollectBO.setKnowledgeName(knowledge.getKnowledgeName());
            userCollectBOList.add(userCollectBO);
        }
        return userCollectBOList;
    }
}
